package com.linwen.excise.leetcode;

import java.util.HashMap;

public class _3无重复字符的最长子串 {
    public static void main(String[] args) {
        String s = "abcdadca";
        System.out.println(lengthOfLongestSubstring(s));
    }
    public static int lengthOfLongestSubstring(String s) {
        int start = 0;
        int end = 0;
        int ans = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        for (; end < s.length(); end++) {
            char alpha = s.charAt(end);
            if (map.containsKey(alpha)) {
                start = Math.max(map.get(alpha), start);
            }
            ans = Math.max(ans, end - start + 1);
            map.put(s.charAt(end), end + 1);
        }
        return ans;
    }
}
